package com.gexing.hostedit;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public class HostEditActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void clickButton(View v) {
		switch (v.getId()) {
		case R.id.main_bt_add_online:
			gaiHost(true);
			break;
		case R.id.main_bt_add_214:
			gaiHost(false);
			break;
		}
	}

	private void gaiHost(boolean isOnline) {
		File file = new File("/etc/hosts");

		// 如果需要修改文件权限则 将 以下字符串传入以下方法 777则是 rwx-rwx-rwx权限
		String cmd = "chmod 777 " + java.io.File.separator + "etc" + java.io.File.separator + "hosts";

		boolean rootCommand = RootCommand(cmd);
		Log.i("gp", "cmd :" + rootCommand);

		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
			if(!isOnline){
				bw.write("172.16.1.214 shouji.gexing.com\n");
				bw.write("172.16.1.214 sso.gexing.com\n");
				bw.write("172.16.1.214 pic.upload.gexing.com\n");
				bw.write("172.16.1.214 pic.tmp.gexing.com\n");
				bw.write("172.16.1.214 p1.gexing.com\n");
				bw.write("172.16.1.214 p2.gexing.com\n");
				bw.write("172.16.1.214 p3.gexing.com\n");
				bw.write("172.16.1.214 p4.gexing.com\n");
				bw.write("172.16.1.214 a1.gexing.com\n");
				bw.write("172.16.1.214 a2.gexing.com\n");
				bw.write("172.16.1.214 a3.gexing.com\n");
				bw.write("172.16.1.214 a4.gexing.com\n");
			}
			bw.flush();
			bw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private String read(InputStream ips) {
		BufferedReader br = null;
		StringBuilder sb = null;
		try {
			br = new BufferedReader(new InputStreamReader(ips));
			sb = new StringBuilder();
			String str;
			while ((str = br.readLine()) != null) {
				sb.append(str);
			}
		} catch (IOException e) {
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
				}
			}
		}
		if (sb != null) {
			return sb.toString();
		} else {
			return "";
		}
	}

	// 如果需要修改文件权限则 将 以下字符串传入以下方法 777则是 rwx-rwx-rwx权限
	// String cmd= "chmod 777 " + java.io.File.separator + "data"
	// + java.io.File.separator + "system" + java.io.File.separator
	// + "accounts.db";

	public boolean RootCommand(String command) {
		Process process = null;
		DataOutputStream os = null;
		try {
			process = Runtime.getRuntime().exec("su");
			os = new DataOutputStream(process.getOutputStream());
			os.writeBytes(command + "\n");
			os.writeBytes("exit\n");
			os.flush();
			process.waitFor();
		} catch (Exception e) {
			Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
			return false;
		} finally {
			try {
				if (os != null) {
					os.close();
				}
				process.destroy();
			} catch (Exception e) {
				// nothing
			}
		}

		Log.d("*** DEBUG ***", "Root SUC ");
		return true;

	}

}